//-----------------------------------------------------------------------
// <copyright file="AddMoreSellProductDAOBase.cs" company="FetCo">
//     Copyright (c) FetCo. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Data;
using System.Data.Common;
using System.Collections;
using FetCo.Framework;
using FetCo.Framework.Data;
using Microsoft.Practices.EnterpriseLibrary.Data;
using ADESSA.Entities;

namespace ADESSA.Data
{    
    public abstract class AddMoreSellProductDAOBase
    {
        #region Common methods
        public virtual AddMoreSellProduct CreateAddMoreSellProductFromReader(IDataReader reader)
        {
            AddMoreSellProduct item = new AddMoreSellProduct();
            try
            {
                if (!reader.IsDBNull(reader.GetOrdinal("AddMoreId"))) item.AddMoreId = (long)reader["AddMoreId"];
                if (!reader.IsDBNull(reader.GetOrdinal("Name"))) item.Name = (string)reader["Name"];
                if (!reader.IsDBNull(reader.GetOrdinal("Title"))) item.Title = (string)reader["Title"];
                if (!reader.IsDBNull(reader.GetOrdinal("ProductId"))) item.ProductId = (long)reader["ProductId"];
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessCreateAddMoreSellProductFromReaderException, ex);
            }
            return item;
        }
        #endregion
        
        #region CreateAddMoreSellProduct methods
            
        public virtual void CreateAddMoreSellProduct(AddMoreSellProduct addMoreSellProduct)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spAddMoreSellProductInsert");
                
                database.AddInParameter(dbCommand, "@Name", DbType.String, addMoreSellProduct.Name);
                database.AddInParameter(dbCommand, "@Title", DbType.String, addMoreSellProduct.Title);
                database.AddInParameter(dbCommand, "@ProductId", DbType.Int64, addMoreSellProduct.ProductId);
                database.AddOutParameter(dbCommand, "@AddMoreId", DbType.Int64, 8);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
                addMoreSellProduct.AddMoreId = (long)database.GetParameterValue(dbCommand, "@AddMoreId");
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessCreateAddMoreSellProductException, ex);
            }
        }

        #endregion

        #region UpdateAddMoreSellProduct methods
        
        public virtual void UpdateAddMoreSellProduct(AddMoreSellProduct addMoreSellProduct)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spAddMoreSellProductUpdate");            
                
                database.AddInParameter(dbCommand, "@AddMoreId", DbType.Int64, addMoreSellProduct.AddMoreId);
                database.AddInParameter(dbCommand, "@Name", DbType.String, addMoreSellProduct.Name);
                database.AddInParameter(dbCommand, "@Title", DbType.String, addMoreSellProduct.Title);
                database.AddInParameter(dbCommand, "@ProductId", DbType.Int64, addMoreSellProduct.ProductId);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessUpdateAddMoreSellProductException, ex);
            }
        }
        
        #endregion

        #region DeleteAddMoreSellProduct methods
        public virtual void DeleteAddMoreSellProduct(long addMoreId)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spAddMoreSellProductDelete");
                
                database.AddInParameter(dbCommand, "@AddMoreId", DbType.Int64, addMoreId);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessDeleteAddMoreSellProductException, ex);
            }
        }

        #endregion

        #region GetAddMoreSellProduct methods
        
        public virtual AddMoreSellProduct GetAddMoreSellProduct(long addMoreId)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spAddMoreSellProductGet");
                
                database.AddInParameter(dbCommand, "@AddMoreId", DbType.Int64, addMoreId);
                
                AddMoreSellProduct addMoreSellProduct =  null;
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    if (reader.Read())
                    {
                        addMoreSellProduct = CreateAddMoreSellProductFromReader(reader);
                        reader.Close();
                    }
                }
                return addMoreSellProduct;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGetAddMoreSellProductException, ex);
            }
        }
        
        #endregion

        #region GetAddMoreSellProductList methods
        public virtual AddMoreSellProductCollection GetAddMoreSellProductList(AddMoreSellProductColumns orderBy, OrderDirection orderDirection, int page, int pageSize, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spAddMoreSellProductGetList");
                
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);
                
                AddMoreSellProductCollection addMoreSellProductCollection = new AddMoreSellProductCollection();
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    while (reader.Read())
                    {
                        AddMoreSellProduct addMoreSellProduct = CreateAddMoreSellProductFromReader(reader);
                        addMoreSellProductCollection.Add(addMoreSellProduct);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return addMoreSellProductCollection;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGetAddMoreSellProductListException, ex);
            }
        }
        
        public virtual AddMoreSellProductCollection GetAddMoreSellProductList(AddMoreSellProductColumns orderBy, OrderDirection orderDirection)
        {            
            int totalRecords = 0;
            return GetAddMoreSellProductList(orderBy, orderDirection, 0, 0, out totalRecords);
        }
        
        #endregion
		
		  #region GetAddMoreSellProductList methods
        public virtual AddMoreSellProductCollection GetAddMoreSellProductListByParam(object orderBy, OrderDirection orderDirection, int page, int pageSize,string Criteria, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spAddMoreSellProductGetListByParam");
                
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
				database.AddInParameter(dbCommand, "@Criteria",  DbType.String, Criteria);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);
                
                AddMoreSellProductCollection addMoreSellProductCollection = new AddMoreSellProductCollection();
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    while (reader.Read())
                    {
                        AddMoreSellProduct addMoreSellProduct = CreateAddMoreSellProductFromReader(reader);
                        addMoreSellProductCollection.Add(addMoreSellProduct);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return addMoreSellProductCollection;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGetAddMoreSellProductListException, ex);
            }
        }
        
        public virtual AddMoreSellProductCollection GetAddMoreSellProductListByParam(object orderBy, OrderDirection orderDirection,string Criteria)
        {            
            int totalRecords = 0;
            return GetAddMoreSellProductListByParam(orderBy, orderDirection, 0, 0,Criteria, out totalRecords);
        }
        
        #endregion
    }
}